const fs = require('fs');
const path = require('path');

function args(){
    const argarr = process.argv.slice(3)
    console.log(process.argv)
    const re = {}
    for( let i in argarr){
        let arr = argarr[i].split(":")
        if( arr.length<2 ){
            continue
        }
        re[arr[0].replace("--", '')] = arr[1]
    }
    return re;
}
// 判断文件是否存在的函数
function fileExists(filePath) {
    try {
      return fs.statSync(filePath).isFile();
    } catch (err) {
      return false;
    }
}

function readFilesContent( dir, type = '.html'){
    const directoryPath = dir; // 替换为您的文件夹路径
    const fileExtension = type; // 指定文件类型
    const contents = [];
    try {
      const files = fs.readdirSync(directoryPath);
      const filteredFiles = files.filter(file => path.extname(file) === fileExtension || type=='*');
     
      filteredFiles.forEach(file => {
        const filePath = path.join(directoryPath, file);
        const content = fs.readFileSync(filePath, 'utf-8');
        contents.push(content)
      });
    } catch (error) {
      console.error('Error reading files:', error);
    }
    return contents;

}

async function deleteDirectoryRecursive(directoryPath, type="*") {
    if (fs.existsSync(directoryPath)) {
      fs.readdirSync(directoryPath).forEach(function(file, index) {
        var curPath = path.join(directoryPath, file);
        if (fs.lstatSync(curPath).isDirectory()) { // recurse
          deleteDirectoryRecursive(curPath);
        } else { // delete file
          if (type == "*" || path.extname(curPath)==type) fs.unlinkSync(curPath);
        }
      });
      if(type == '*') fs.rmdirSync(directoryPath);
    }
}

module.exports.args = args
module.exports.fileExists = fileExists
module.exports.readFilesContent = readFilesContent
module.exports.deleteDirectoryRecursive = deleteDirectoryRecursive